package com.requestfactory.wire.hello;

import junit.framework.Assert;
import junit.framework.TestCase;

import com.requestfactory.wire.Wire;
import com.requestfactory.wire.WireSkeletonException;
import com.requestfactory.wire.WireStubException;

public class HelloServiceTestCase extends TestCase {
	
	public void testHelloService() 
	throws WireSkeletonException, WireStubException, HelloServiceException {
		int port = 2680;
		String context = "/hello";
		//
		// create skeleton HTTP server on port and context above 
		// bind service interface with implementation
		//
		Wire.INSTANCE.getSkeleton(port, context).bindService(
				HelloService.class, new HelloServiceImpl());
		//
		// create stub (proxy) for remote service created above
		//
		HelloService helloServiceStub = Wire.INSTANCE.createStub(
				HelloService.class, 
				"http://localhost:" + port + context);
		//
		// invoke service remotely over HTTP
		//
		String name = "Wire Service";
		String actual = helloServiceStub.hello(name);
		Assert.assertEquals("Hello " + name + "!", actual);
		//
		// catch remote service exception example
		//
		try {
			helloServiceStub.hello(null);
			Assert.assertTrue(false);
		} catch(HelloServiceException ex) {
			Assert.assertTrue(true);
		}
	}
	
	public static void main(String... args) 
	throws WireSkeletonException, WireStubException, HelloServiceException {
		int port = 2680;
		String context = "/hello";
		// --- create skeleton HTTP server on port and context above 
		// --- bind service interface with implementation
		Wire.INSTANCE.getSkeleton(port, context).bindService(
				HelloService.class, new HelloServiceImpl());
		// --- create stub (proxy) for remote service created above
		HelloService helloServiceStub = Wire.INSTANCE.createStub(
				HelloService.class, 
				"http://localhost:" + port + context);
		// --- invoke service remotely over HTTP
		String name = "Wire Service";
		String result = helloServiceStub.hello(name);
		System.out.println(result);
	}
}
